import { Injectable } from '@angular/core';
import {Observable, Subject} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class EventbusService {
  private subjects: Subject<any>[] = [];

  constructor() { }

  publish(eventName: string) {
// ensure a subject for the event name exists
    // @ts-ignore
    this.subjects[eventName] = this.subjects[eventName] || new Subject();

// publish event
    // @ts-ignore
    this.subjects[eventName].next();
  }

  //添加消息接收方法
  on(eventName: string): Observable<any> {
// ensure a subject for the event name exists
    // @ts-ignore
    this.subjects[eventName] = this.subjects[eventName] || new Subject();

// return observable
    // @ts-ignore
    return this.subjects[eventName].asObservable();
  }

}
